package com.mairuide.exceltitle_dict.service;

import com.mairuide.exceltitle_dict.pojo.ExceltitleDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 敲代码的卡卡罗特
 * on 2019/1/10 10:55.
 */
@Service
public class ExceltitleDictService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<ExceltitleDict> getListByPackageName(String packageName){
        String sql="select CLASS_NAME className,CLASS_FIELD classField,field_type fieldType,BANK_TITLE_NAME titleValues,ID id from TB_DEAL_FLOW_TITLE_POOL WHERE CLASS_NAME= '"+packageName+"' ";
        List<ExceltitleDict> ExceltitleDictList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<ExceltitleDict>(ExceltitleDict.class));
        return ExceltitleDictList;
    }

    /**
     * 根据标题头返回对应的数据库的行对象
     * @param titles
     * @return
     */
    public Map<Integer, ExceltitleDict> getExceltitleDictByTitleHead(List<String> titles,String packageName){
        Map<Integer, ExceltitleDict> map = new HashMap<>();
        List<ExceltitleDict> ExceltitleDicts = getListByPackageName(packageName);
        for (int k=0;k<titles.size();k++){
            String s = titles.get(k);
            for(int i=0;i<ExceltitleDicts.size();i++){
                ExceltitleDict exceltitleDict = ExceltitleDicts.get(i);
                String titleValues = exceltitleDict.getTitleValues();
                if (titleValues.equals(s)){
                    map.put(k,exceltitleDict);
                    break;
                }
            }
        }
        return map;
    }
}
